package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.MyTree;
import com.ruoyi.system.domain.ApplySet;
import com.ruoyi.system.domain.ApplyTable;
import com.ruoyi.system.domain.DutyInfo;
import com.ruoyi.system.service.IApplySetService;
import com.ruoyi.system.service.IApplyTableService;
import com.ruoyi.system.service.IDutyInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OrderInfo;
import com.ruoyi.system.service.IOrderInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 * @date 2021-11-13
 */
@Controller
@RequestMapping("/order/info2")
public class OrderInfo2Controller extends BaseController
{
    private String prefix = "order/info2";

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private IApplySetService applySetService;

    @Autowired
    private IApplyTableService applyTableService;

    @Autowired
    private IDutyInfoService dutyInfoService;

    @RequiresPermissions("order:info:view")
    @GetMapping()
    public String info()
    {
        return prefix + "/info2";
    }

    /**
     * 查询预约信息列表
     */
    @RequiresPermissions("order:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderInfo orderInfo)
    {
        startPage();
        if(orderInfoService.selectOrderInfo3List(orderInfo).size() == 0){
            return getDataTable(new ArrayList<OrderInfo>());
        }
        List<OrderInfo> list = orderInfoService.selectOrderInfo2List(orderInfo);
        return getDataTable(list);
    }

    @PostMapping("/list2")
    @ResponseBody
    public AjaxResult list2(OrderInfo orderInfo)
    {
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        return AjaxResult.success(list);
    }

    /**
     * 导出预约信息列表
     */
    @RequiresPermissions("order:info:export")
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderInfo orderInfo)
    {
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
        return util.exportExcel(list, "预约信息数据");
    }

    /**
     * 新增预约信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 表格查询
     */
    @GetMapping("/queryTable/{id}")
    public String queryTable(@PathVariable("id") Long id, ModelMap mmap)
    {
        ApplySet applySet = applySetService.selectApplySetById(id);
        ApplyTable applyTable = new ApplyTable();
        applyTable.setParentid("0");
        List<MyTree> list = applyTableService.selectApplyTableMyTree(applyTable);
        List<MyTree> list1 = applyTableService.selectApplyTableMyTree(applySet.getSet());
        list.addAll(list1);
        list = MyTree.CombinationTree(0L,list);
        applySet.setTableSet(list);
        mmap.addAttribute("tableInfo",applySet);

        return prefix + "/table";
    }

    /**
     * 新增保存预约信息
     */
    @RequiresPermissions("order:info:add")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OrderInfo orderInfo)
    {
        Long num = orderInfoService.CountOrderedNum(orderInfo.getUid());
        if( num >= 1){
            return error("未结束预约课程不能大于1节，目前预约"+ num + "节课");
        }
        orderInfo.setStatus(1L);
        return toAjax(orderInfoService.insertOrderInfo(orderInfo));
    }

    /**
     * 修改预约信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OrderInfo orderInfo = orderInfoService.selectOrderInfoById(id);
        mmap.put("orderInfo", orderInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存预约信息
     */
    @RequiresPermissions("order:info:edit")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrderInfo orderInfo)
    {
        Date date = new Date();
        // 当状态老师确定预约后，取消这个老师其他的所有的上班表
        if(2 == orderInfo.getStatus()){
            orderInfo = orderInfoService.selectOrderInfoById(orderInfo.getId());
            orderInfo.setStatus(2L);
            DutyInfo dutyInfo = new DutyInfo();
            dutyInfo.setPid(orderInfo.getPid());
            dutyInfo.setDay(orderInfo.getDay());
            dutyInfo.setTime(orderInfo.getTime());
            dutyInfoService.deleteDutyInfoByTimePidDay(dutyInfo);
        }
        // 申请发起的时候记录发起的时间
        else if(1 == orderInfo.getStatus()){
            orderInfo.setCreateTime(date);
        }
        // 老师确认填表之后，记录预约完成的时间
        else if(4 == orderInfo.getStatus()){
            orderInfo.setOrderTime(date);
        }
        // 活动完成之后，记录活动的完成时间
        else if(5 == orderInfo.getStatus()){
            orderInfo.setOrderTime(date);
        }
        return toAjax(orderInfoService.updateOrderInfo(orderInfo));
    }

    /**
     * 删除预约信息
     */
    @RequiresPermissions("order:info:remove")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orderInfoService.deleteOrderInfoByIds(ids));
    }

    /**
     *  预约延期时间
     */
    @PostMapping( "/delayOrder")
    @ResponseBody
    public AjaxResult x(Long id,String delayTime)
    {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setDelayTime(delayTime);
        orderInfo.setId(id);
        return toAjax(orderInfoService.updateOrderInfo(orderInfo));
    }
}
